package com.bcu.model.service;

import cn.hutool.json.JSONUtil;
import com.bcu.model.sdk.MyConfig;
import com.bcu.model.sdk.WXPay;
import com.bcu.model.util.PayUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author:Wls
 * @Date:8:56 2019/12/10
 * @Description: 微信支付
 */
@Configuration
@Component
public class WeChatPayStrategy extends PayStrategy {

    @Value("${WxPay.appid}")
    private String appid;

    @Value("${WxPay.spbillcreateIp")
    private String spbillcreateIp;

    @Value("${WxPay.notifyUrl}")
    private String notifyUrl;

    @Value("${WxPay.key}")
    private String key;


    @Override
    public String doPay(Map<String,String> param){
        try {
            MyConfig myConfig=new MyConfig();
            WXPay wxPay=new WXPay(myConfig);

            param.put("out_trade_no", PayUtil.getOutTradeNo());
            param.put("fee_type","CNY");
            param.put("spbill_create_ip",PayUtil.getSpbillCreateIp());
            param.put("notify_url",PayUtil.getNotifyUrl());
            param.put("trade_type","JSAPI");


            System.out.println("微信支付");
            System.out.println("从配置文件读取到的数据");
            System.out.println(JSONUtil.parseObj(param).toString());
            String rs=JSONUtil.parseObj(wxPay.unifiedOrder(param)).toString();
            System.out.println("微信支付官方返回的数据："+rs);
            return rs;
        }catch (Exception e){
            e.printStackTrace();
        }
       return "ERROR!";


    }
}
